<?php


namespace app\service;

use Intervention\Image\ImageManagerStatic as Image;

class Upload
{

    protected $config = [
        'up_dir' => './upload/admin/images',//储存目录
        'allow_exts' => ['jpg', 'jpeg', 'gif', 'png', 'bmp', 'webp'], // 允许上传的文件后缀
        'zip_image' => 0, //是否压缩图片大小 1 按设置压缩 0 不压缩
        'thumb_size' => [
//            ['width' => 675, 'height' => 675],
//            ['width' => 350, 'height' => 350]
        ], //图片压缩尺寸
    ];

    public function __construct(array $config = [])
    {
        if(!empty( $config['up_dir'])){
            $this->config['up_dir'] = $config['up_dir'];
        }
        if(!empty( $config['allow_exts'])){
            $this->config['allow_exts'] = $config['allow_exts'];
        }
        if(!empty( $config['zip_image'])){
            $this->config['zip_image'] = $config['zip_image'];
        }
        if(!empty( $config['image_max_width'])){
            $this->config['image_max_width'] = $config['image_max_width'];
        }
        if(!empty( $config['image_max_height'])){
            $this->config['image_max_height'] = $config['image_max_height'];
        }
    }


    /**
     * @param string $base64_img
     * Desc: base64图片上传
     * User: 苏鹏
     * Date: 2023/9/22
     * Time: 22:39
     * @return mixed
     * @throws \think\Exception
     */
    public function fileBase64(string $base64_img)
    {

        if (!file_exists($this->config['up_dir'])) {
            mkdir($this->config['up_dir'], 0777, true);
        }

        $verify_base64 = preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result);
        if (empty($verify_base64))
        {
            re_error_json(msg_code(304)); //文件错误
        }

        $type = $result[2];
        if (!in_array($type, $this->config['allow_exts']))
        {
            re_error_json(msg_code(303)); //文件类型错误
        }

        $img_url = $this->config['up_dir'] .'/'. date('YmdHis') .'_'.rand(1,1000). '.' . $type;

        if (file_put_contents($img_url, base64_decode(str_replace($result[1], '', $base64_img))))
        {

            //生成缩略图
            $thumb_img_url = [];
            if($this->config['zip_image'] == 1){
                //拼接保存路径
                $extension = pathinfo($img_url, PATHINFO_EXTENSION);
                $thumb_img_path = basename($img_url, '.'.$extension);
                foreach($this->config['thumb_size'] as $k=>$v){
                    $thumb_img = $this->config['up_dir'].'/'.$thumb_img_path.'_'.$v['width'].'x'.$v['height'].'.'.$extension;
                     Image::make($img_url)->resize($v['width'], $v['height'])->save($thumb_img_path);
                    $thumb_img_url[] = return_upload_url($thumb_img);
                }
            }

            return [
                'img' => trim($img_url, '.'),
                'img_url' => return_upload_url($img_url),
                'thumb_img_url' => $thumb_img_url
            ];
        } else {
            re_error_json(msg_code(20));  //图片资源写入文件失败
        }
    }

    /**
     * param $path 文件地址
     * Desc: 删除本地文件
     * User: 苏鹏
     * Date: 2023/10/7
     * Time: 13:45
     * @return void
     */
    public function delLocalFile(string $path)
    {
        if(empty($path)){
            return;
        }
        if(file_exists($path))
        {
            unlink($path);
        }
    }
}